# str
# list
# 内置函数：dir()
# print(dir(str))
# 判断一个对象是否是可迭代对象：
# print('__iter__' in dir(str))
[1,2,3]
# {'name': 'alex'}
# 'fdskjalfd'
#

# 迭代器：
# with open('调试',encoding='utf-8',mode='w') as f1:
# #     print(f1)

# print(zip('fdsa','fdsagdfsg'))  # <zip object at 0x00000000029B1D88>

# 可迭代对象转化成迭代器：
# l1 = [1, 2, 3, 4, 5]
# # 内置函数iter()
# obj = iter(l1)  # l1.__iter__()
# print(obj)  # <list_iterator object at 0x0000000001DEA2E8>

# 迭代器可以迭代取值。利用next()进行取值
l1 = [1, 2, 3, 4, 5]
# 内置函数iter()
obj = iter(l1)
# print(obj)
# print(next(obj))
# print(next(obj))
# print(next(obj))
# print(next(obj))
# print(next(obj))
# print(next(obj))

# 迭代器一条路走到底，不走回头（记录位置）。

l1 = [22, 33, 44, 55, 66, 77]
obj = iter(l1)

for i in range(3):
    print(next(obj))

for i in range(2):
    print(next(obj))

# 迭代器：

# 利用while循环，模拟for循环内部循环可迭代对象的机制。
#1，先要将可迭代对象转化成迭代器。
#2. 利用next对迭代器进行取值。
#3. 利用异常处理try一下防止报错。
# try:
#     l1 = [1,]
#     # l1[::2]
#     l1[100]
#     # IndexError
# except IndexError:
#      pass
# StopIteration
# s = 'gkffdsa;lfkdsk;lafkds;laldgjfd'
# obj = iter(s)
# while 1:
#     try:
#         print(next(obj))
#     except StopIteration:
#         break
#


